package j2se.socket;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Created by jingqing.zhou on 2015/6/17.
 * 基于tcp协议的socket通信，实现用户登录
 * 服务器端
 */
public class server {
    public static void main(String[] args) {
        //1.创建一个服务器端socket
        try {
            ServerSocket sst =  new ServerSocket(8999);
            System.out.println("服务器即将启动，等待客户端的链接=====>");
            //记录客户端的数量
            int count = 0 ;
            while (true){
                Socket socket = sst.accept();//阻塞，等待客户端请求
                //创建一个新的线程
                ServerThread serverThread = new ServerThread(socket);
                //启动线程
                serverThread.start();
                count ++ ;
                System.out.println("当前客户连接数"+count);
                InetAddress address = socket.getInetAddress();
                System.out.println("当前客户端的ip地址："+address.getHostAddress());

            }

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
